/**
 * 拦截专用 store
 */
import { isLogin } from '@/api/login'
const state = {
    menuList: [],
    btnList: [],
    isLogin: false, // 是否在租户用户已经登录了
    currentUser: false
}

const mutations = {
    SET_MENU_LIST: (state, menuList, type) => {
        getMenuData(menuList, state.menuList, state.btnList)
        state.isLoadedMenu = true
    },
    SET_USER_LOGIN: (state, login) => {
        state.isLogin = !login ? false : login.transit;
        state.currentUser = !login ? false : login.current_user || false;
    },
}

const actions = {
    isLogin({ commit }) {
        return new Promise((resolve, reject) => {
            isLogin().then(res => {
                commit('SET_USER_LOGIN', {transit:true,current_user:'小明'})
                resolve(res)
            }).catch(err => {
                reject(err)
            })
        })
    }
}
function getMenuData(data, menus = [], btns = []) {
    if (!data || !Array.isArray(data)) return
    data.forEach(e => {
      if (e.type === 0) {
        menus.push(e.permCode)
      } else if (e.type === 2) {
        btns.push(e.permCode)
      }
      getMenuData(e.childs, menus, btns)
    })
  }
export default {
    namespaced: true,
    state,
    mutations,
    actions
}